package com.webserver.servlet;
import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class register extends HttpServlet{
    public void service(HttpRequest httpRequest,HttpResponse httpResponse){
        //1.获取数据
        String username = httpRequest.getQueryMap("username");
        String password = httpRequest.getQueryMap("password");
        String date = httpRequest.getQueryMap("date");
        String age = httpRequest.getQueryMap("age");
        if(username==null||password==null||age==null||!age.matches("\\d+")) {
            httpResponse.setFile(new File("./Webapps/myweb/error.html"));
            return;
        }
        //2.写入数据
        //判断用户名是否存在
        try (Connection conn = Jdbc.getCon()){
            String sql = "insert into user values(null,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.executeUpdate();
            //在客户端显示注册成功!
            File file = new File("./webapps/myweb/register_success.html");
            httpResponse.setFile(file);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
            System.out.println("注册完毕!");
    }
}
